package com.itheima.reggie.controller;

import cn.hutool.db.sql.Order;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.AddressBook;
import com.itheima.reggie.entity.OrderDetail;
import com.itheima.reggie.entity.Orders;
import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.service.AddressBookService;
import com.itheima.reggie.service.OrderDetailService;
import com.itheima.reggie.service.OrdersService;
import com.itheima.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @Description:
 * @Title: OrderController
 * @Package com.itheima.reggie.controller
 * @Author: 🐟
 * @Copyright 🐟
 * @CreateTime: 2022/9/16 19:09
 */
@RestController
@Slf4j
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private AddressBookService addressBookService;
    @Autowired
    private OrderDetailService orderDetailService;
    @Autowired
    private OrdersService ordersService;
    @Autowired
    private ShoppingCartService shoppingCartService;
    @Autowired
    private HttpSession session;

    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders) {
        //先向orders中填充数据

        Long user = (Long) session.getAttribute("User");
        Long orderId = IdWorker.getId();//订单号\
        String s = orderId.toString();
        orders.setNumber(s);
        AddressBook byId = addressBookService.getById(orders.getAddressBookId());
        orders.setPhone(byId.getPhone());
        orders.setConsignee(byId.getConsignee());
        orders.setAddress(byId.getDetail());
        orders.setUserId(user);
        orders.setStatus(2);
        orders.setUserName("林之迷");
        orders.setOrderTime(LocalDateTime.now());
        orders.setCheckoutTime(LocalDateTime.now());
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId, user);
        List<ShoppingCart> list = shoppingCartService.list(lqw);
        BigDecimal bigDecimal = new BigDecimal(0);
        for (ShoppingCart shoppingCart : list) {
            Integer number = shoppingCart.getNumber();
            BigDecimal bigDecimal1 = new BigDecimal(number);
            BigDecimal amount = shoppingCart.getAmount();
            BigDecimal multiply = bigDecimal1.multiply(amount);
            bigDecimal = bigDecimal.add(multiply);
        }
        orders.setAmount(bigDecimal);
        ordersService.save(orders);
        //在向OrderDetail添加数据
        List<OrderDetail> OrderDetail1 = list.stream().map(shoppingCart -> {
            OrderDetail orderDetail = new OrderDetail();
            BeanUtils.copyProperties(shoppingCart, orderDetail, "id");
            orderDetail.setOrderId(orders.getId());
            return orderDetail;
        }).collect(Collectors.toList());
        orderDetailService.saveBatch(OrderDetail1);
        //清空购物车
        shoppingCartService.remove(lqw);
        return R.success("清空购物车成功");
    }

    @GetMapping("/userPage")
    public R<Page<Orders>> getdrders(int page, int pageSize) {
        LambdaQueryWrapper<Orders> lqw = new LambdaQueryWrapper<>();
        lqw.orderByDesc(Orders::getOrderTime);
        Page<Orders> ordersPage = new Page<>(page, pageSize);
        Page<Orders> page1 = ordersService.page(ordersPage,lqw);
        return R.success(page1);
    }

    @GetMapping("/page")
    public R<Page<Orders>> getdrders2(int page, int pageSize) {
        LambdaQueryWrapper<Orders> lqw = new LambdaQueryWrapper<>();
        lqw.orderByDesc(Orders::getOrderTime);
        Page<Orders> ordersPage = new Page<>(page, pageSize);
        Page<Orders> page1 = ordersService.page(ordersPage,lqw);
        return R.success(page1);
    }
}
